{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e31eea60",
   "metadata": {},
   "source": [
    "## 索引\n",
    "- 从前面是0开始\n",
    "- 从后面是-1开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c01e2cf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'t'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang =\"tang yu di\"\n",
    "tang[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "46cd21da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'y'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76691ea3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'i'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4c6b868f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'y'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[-5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1b8aa9e7",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m tang[\u001b[38;5;241m10\u001b[39m]\n",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "tang[10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec82af10",
   "metadata": {},
   "source": [
    "\n",
    "### 切片\n",
    "： 表示从哪到哪，作弊又开的区间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a7c14fa6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tang'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[0:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "605216b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yu di'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[5:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b5dcedbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ang yu '"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[1:-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "60828343",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' di'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[-3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dae2f2f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tang yu di'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6dfb5b30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tn ud'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cf6f64e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
